import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/mood_provider.dart';
import '../providers/app_state_provider.dart';
import '../widgets/mood_card.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  final PageController _pageController = PageController();
  int _currentPage = 0;

  // 模拟多个卡片数据
  final List<Map<String, dynamic>> _cardData = [
    {
      'greeting': '亲爱的，早上好！',
      'weather': '☀️',
      'temp': '28°',
      'weatherDesc': '晴转多云',
      'bgColors': [Color(0xFFA2D2FF), Color(0xFF6366F1)],
    },
    {
      'greeting': '晚上好呀！',
      'weather': '🌙',
      'temp': '25°',
      'weatherDesc': '多云',
      'bgColors': [Color(0xFF03045E), Color(0xFF023E8A)],
    },
    {
      'greeting': '下午好！',
      'weather': '⛅',
      'temp': '26°',
      'weatherDesc': '阴转晴',
      'bgColors': [Color(0xFFFF6B9D), Color(0xFFC44569)],
    },
  ];

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Consumer2<MoodProvider, AppStateProvider>(
      builder: (context, moodProvider, appProvider, child) {
        return Container(
          decoration: const BoxDecoration(
            gradient: LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              colors: [
                Color(0xFFF9FAFB),
                Color(0xFFF3F4F6),
              ],
            ),
          ),
          child: Stack(
            children: [
              // 卡片PageView
              PageView.builder(
                controller: _pageController,
                onPageChanged: (index) {
                  setState(() {
                    _currentPage = index;
                  });
                },
                itemCount: _cardData.length,
                itemBuilder: (context, index) {
                  return MoodCard(
                    cardData: _cardData[index],
                  );
                },
              ),
              
              // 左右滑动按钮
              Positioned(
                left: 16,
                top: 0,
                bottom: 0,
                child: Center(
                  child: _currentPage > 0
                      ? GestureDetector(
                          onTap: () {
                            _pageController.previousPage(
                              duration: const Duration(milliseconds: 300),
                              curve: Curves.easeInOut,
                            );
                          },
                          child: Container(
                            width: 40,
                            height: 40,
                            decoration: BoxDecoration(
                              color: Colors.white.withOpacity(0.8),
                              borderRadius: BorderRadius.circular(20),
                              boxShadow: [
                                BoxShadow(
                                  color: Colors.black.withOpacity(0.1),
                                  blurRadius: 8,
                                  offset: const Offset(0, 2),
                                ),
                              ],
                            ),
                            child: const Icon(
                              Icons.chevron_left,
                              color: Color(0xFF6B7280),
                              size: 24,
                            ),
                          ),
                        )
                      : const SizedBox(),
                ),
              ),
              
              Positioned(
                right: 16,
                top: 0,
                bottom: 0,
                child: Center(
                  child: _currentPage < _cardData.length - 1
                      ? GestureDetector(
                          onTap: () {
                            _pageController.nextPage(
                              duration: const Duration(milliseconds: 300),
                              curve: Curves.easeInOut,
                            );
                          },
                          child: Container(
                            width: 40,
                            height: 40,
                            decoration: BoxDecoration(
                              color: Colors.white.withOpacity(0.8),
                              borderRadius: BorderRadius.circular(20),
                              boxShadow: [
                                BoxShadow(
                                  color: Colors.black.withOpacity(0.1),
                                  blurRadius: 8,
                                  offset: const Offset(0, 2),
                                ),
                              ],
                            ),
                            child: const Icon(
                              Icons.chevron_right,
                              color: Color(0xFF6B7280),
                              size: 24,
                            ),
                          ),
                        )
                      : const SizedBox(),
                ),
              ),
              
              // 页面指示器
              Positioned(
                bottom: 20,
                left: 0,
                right: 0,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: List.generate(
                    _cardData.length,
                    (index) => AnimatedContainer(
                      duration: const Duration(milliseconds: 300),
                      margin: const EdgeInsets.symmetric(horizontal: 4),
                      width: _currentPage == index ? 24 : 8,
                      height: 8,
                      decoration: BoxDecoration(
                        color: _currentPage == index
                            ? const Color(0xFFEC4899)
                            : Colors.white.withOpacity(0.5),
                        borderRadius: BorderRadius.circular(4),
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}
